package com.sgb.kjwl.view.ui.adapter.packers;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.PopularWorkersEntity;
import com.sgb.kjwl.utils.NumberConstant;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;
import java.util.Locale;

/**
*@author 李昭
*@date 2020/9/18
*description:
*/
public class PopularWorkersAdapter extends BaseRecycleAdapter<PopularWorkersEntity> {

    private RequestManager requestManager;

    public PopularWorkersAdapter(Context context, List<PopularWorkersEntity> data, RequestManager requestManager) {
        super(context, R.layout.item_popular_workers, data);
        this.requestManager = requestManager;
    }

    @Override
    public void convert(ItemViewHolder holder, PopularWorkersEntity popularWorkersEntity) {
        holder.setBinding(BR.viewModel, popularWorkersEntity);
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        TextView name1 = holder.itemView.findViewById(R.id.name1);
        RelativeLayout rvTeamPerson = holder.itemView.findViewById(R.id.rv_team_person);
        tvTime.setText(String.format("%s%s",DateUtils.timestampToDate(popularWorkersEntity.getCreateTime()),"发布"));
        RoundedImageView ivPackerAvatar = holder.itemView.findViewById(R.id.iv_packer_avatar);
        TextView tvDepartment = holder.itemView.findViewById(R.id.tv_department);
        String department = "";
        if (popularWorkersEntity.getTeamType() == 1){
            if (TextUtils.isEmpty(popularWorkersEntity.getAffiliatedUnitPic())){
                department = "暂无隶属单位";
            }else if (popularWorkersEntity.getAffiliatedUnitPic().contains("https")){
                department = "暂无隶属单位";
            }else {
                department = popularWorkersEntity.getAffiliatedUnitPic();
            }
        }else if (popularWorkersEntity.getTeamType() == 0){
            if (TextUtils.isEmpty(popularWorkersEntity.getLegalComName())){
                department = "暂无隶属单位";
            }else if (popularWorkersEntity.getLegalComName().contains("https")){
                department = "暂无隶属单位";
            }else {
                department = popularWorkersEntity.getLegalComName();
            }
        }
        tvDepartment.setText(department);
        if (null != popularWorkersEntity.getAvatarAddress() && !TextUtils.isEmpty(popularWorkersEntity.getAvatarAddress())) {
            RequestOptions options = new RequestOptions()
                    .placeholder(popularWorkersEntity.getTeamType() == 0 ? R.mipmap.construction_team_detail: R.mipmap.professional_crew_detail)
                    .transforms(new CenterCrop(), new RoundedCorners(8));
            requestManager.load(popularWorkersEntity.getAvatarAddress()).apply(options).into(ivPackerAvatar);
        } else {
            requestManager.load(popularWorkersEntity.getTeamType() == 0 ? R.mipmap.construction_team_detail: R.mipmap.professional_crew_detail).into(ivPackerAvatar);
        }
        if (TextUtils.isEmpty(popularWorkersEntity.getTeamNumber()) || "0".equals(popularWorkersEntity.getTeamNumber()) || "0人".equals(popularWorkersEntity.getTeamNumber())) {
            rvTeamPerson.setVisibility(View.GONE);
        } else {
            rvTeamPerson.setVisibility(View.VISIBLE);
        }
        String[] strArray = popularWorkersEntity.getCategory().trim().split("\\|");
        tvTime.setText(String.format("%s 发布", DateUtils.timestampToDate(popularWorkersEntity.getCreateTime())));
        name1.setText(String.format(popularWorkersEntity.getTeamTypeName().contains("工队")?"%s-%s-%s":"%s-%s-%s工队",popularWorkersEntity.getContact() , strArray[strArray.length - 1] , popularWorkersEntity.getTeamTypeName()));
    }


    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ImageView imageView = holder.itemView.findViewById(R.id.im_popular_ranking);
        TextView tvRanking = holder.itemView.findViewById(R.id.tv_ranking);
        if (position == 0) {
            requestManager.load(R.mipmap.popular_top_1).into(imageView);
            tvRanking.setText("");
        } else if (position == 1) {
            requestManager.load(R.mipmap.popular_top_2).into(imageView);
            tvRanking.setText("");
        } else if (position == NumberConstant.NUMBER_TWO) {
            requestManager.load(R.mipmap.popular_top_3).into(imageView);
            tvRanking.setText("");
        } else if (position >= NumberConstant.NUMBER_THREE && position < 100) {
            requestManager.load(R.mipmap.popular_ranking).into(imageView);
            tvRanking.setText(String.format(Locale.CHINA,"%d",(position + 1)));
        } else {
            requestManager.load(R.mipmap.popular_ranking).into(imageView);
            tvRanking.setText(String.format(Locale.CHINA,"%s","99+"));
        }

    }


}
